package com.tc.controller;


import com.tc.domain.Hospital_reimbursement_ratio;
import com.tc.dto.Hospital_reimbursement_ratioDTO;
import com.tc.dto.Hospital_reimbursement_ratioQueryDTO;
import com.tc.service.IHospital_reimbursement_ratioService;
import com.tc.utils.ResultVo;
import com.tc.vo.Hospital_reimbursement_ratioVO;
import com.tc.vo.PageResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tc
 * @since 2025-07-09
 */
@RestController
@RequestMapping("/hospital_reimbursement_ratio")
@Api(tags = "医院报销比例管理")
public class Hospital_reimbursement_ratioController {
    
    @Autowired
    private IHospital_reimbursement_ratioService ratioService;
    
    @GetMapping("/page")
    @ApiOperation("分页查询医院报销比例")
    public ResultVo<PageResultVO<Hospital_reimbursement_ratioVO>> page(Hospital_reimbursement_ratioQueryDTO queryDTO) {
        PageResultVO<Hospital_reimbursement_ratioVO> pageResult = ratioService.pageQuery(queryDTO);
        return ResultVo.ok(pageResult);
    }
    
    @GetMapping("/list")
    @ApiOperation("查询全部医院报销比例")
    public ResultVo<List<Hospital_reimbursement_ratioVO>> list(
            @ApiParam(value = "状态，可选") @RequestParam(required = false) String status) {
        List<Hospital_reimbursement_ratioVO> list = ratioService.listAll(status);
        return ResultVo.ok(list);
    }
    
    @GetMapping("/{id}")
    @ApiOperation("根据ID查询医院报销比例")
    public ResultVo<Hospital_reimbursement_ratioVO> getById(@PathVariable("id") Long id) {
        Hospital_reimbursement_ratio ratio = ratioService.getById(id);
        if (ratio == null) {
            return ResultVo.fail("未找到该医院报销比例记录");
        }
        
        Hospital_reimbursement_ratioVO vo = new Hospital_reimbursement_ratioVO();
        BeanUtils.copyProperties(ratio, vo);
        return ResultVo.ok(vo);
    }
    
    @PostMapping
    @ApiOperation("新增医院报销比例")
    public ResultVo<Void> save(@RequestBody Hospital_reimbursement_ratioDTO ratioDTO) {
        Hospital_reimbursement_ratio ratio = new Hospital_reimbursement_ratio();
        BeanUtils.copyProperties(ratioDTO, ratio);
        
        // 设置更新时间
        ratio.setUpdateTime(LocalDateTime.now());
        // 设置删除标志为0（未删除）
        ratio.setDeleted(0);
        
        boolean success = ratioService.save(ratio);
        return success ? ResultVo.ok("新增医院报销比例成功") : ResultVo.fail("新增医院报销比例失败");
    }
    
    @PutMapping
    @ApiOperation("更新医院报销比例")
    public ResultVo<Void> update(@RequestBody Hospital_reimbursement_ratioDTO ratioDTO) {
        if (ratioDTO.getRatioId() == null) {
            return ResultVo.fail("ID不能为空");
        }
        
        // 先查询原记录是否存在
        Hospital_reimbursement_ratio existRatio = ratioService.getById(ratioDTO.getRatioId());
        if (existRatio == null) {
            return ResultVo.fail("未找到该医院报销比例记录");
        }
        
        Hospital_reimbursement_ratio ratio = new Hospital_reimbursement_ratio();
        BeanUtils.copyProperties(ratioDTO, ratio);
        
        // 设置更新时间
        ratio.setUpdateTime(LocalDateTime.now());
        
        boolean success = ratioService.updateById(ratio);
        return success ? ResultVo.ok("更新医院报销比例成功") : ResultVo.fail("更新医院报销比例失败");
    }
    
    @PostMapping("/{id}")
    @ApiOperation("删除医院报销比例")
    public ResultVo<Void> delete(@PathVariable("id") Long id) {
        boolean success = ratioService.removeById(id);
        return success ? ResultVo.ok("删除医院报销比例成功") : ResultVo.fail("删除医院报销比例失败");
    }
    
    @PutMapping("/status/{id}/{status}")
    @ApiOperation("更新医院报销比例状态")
    public ResultVo<Void> updateStatus(
            @ApiParam(value = "ID", required = true) @PathVariable("id") Long id,
            @ApiParam(value = "状态", required = true) @PathVariable("status") String status) {
        
        Hospital_reimbursement_ratio ratio = ratioService.getById(id);
        if (ratio == null) {
            return ResultVo.fail("未找到该医院报销比例记录");
        }
        
        ratio.setStaus(status);
        ratio.setUpdateTime(LocalDateTime.now());
        
        boolean success = ratioService.updateById(ratio);
        return success ? ResultVo.ok("更新医院报销比例状态成功") : ResultVo.fail("更新医院报销比例状态失败");
    }
}

